<template>
	<view class="login">

		<view class="logoandtitle">
			<view class="logobox">
				<image class="logoimg" src="../../static/logo1.jpg"></image>
			</view>
			<view class="title">
				导师双选
			</view>
		</view>

		<view class="protocol">
			<view class="checked">
				<u-checkbox v-model="checked" shape="circle" @change="handleprotocol"></u-checkbox>
			</view>
			<view class="protocoltext">
				已阅读并同意
				<view class="" style="color: #4A9CFF;" @click="gotoXiyi('zhuce')">
					《注册协议》
				</view>
				和
				<view class="" style="color: #4A9CFF;" @click="gotoXiyi('yinsi')">
					《隐私协议》
				</view>
			</view>
		</view>


		<view class="loginbtn" @click="handleLogin">
			微信授权登录
		</view>

	</view>
</template>

<script>
	import {
		login,
		usermessage
	} from "@/api/api.js"
	const app = getApp()
	export default {
		data() {
			return {
				checked: false,
				logincode: "",
				typeFlag: "",
			}
		},
		onLoad() {
			this.getCode() //微信登录获取code码
			this.getTypeFlag()
		},
		methods: {
			async getTypeFlag() {
				let res = await usermessage()
				this.typeFlag = res.data
				console.log("tupeData", res)
				let token = uni.getStorageSync('token')
				// if (token) {
				// 	if (this.typeFlag.identity == 'member') {
				// 		uni.redirectTo({
				// 			url: '/pages/home/index'
				// 		})
				// 	} else {
				// 		uni.redirectTo({
				// 			url: '/pages/home/index'
				// 		})
				// 	}
				// }
			},
			getCode() {
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						console.log("微信获取code码", loginRes);
						this.logincode = loginRes.code
					}
				})
			},

			// async getopenid() {
			// 	let res = await getopenid({
			// 		code: this.logincode
			// 	})
			// 	console.log("openId", res)
			// 	this.openid = res.data.openid
			// },


			handleprotocol() {
				this.checked = !this.checked
				console.log("item", this.checked)
			},
			async handleLogin() {
				let that = this
				if (!that.checked) {
					uni.showToast({
						title: '请先同意《注册协议》和 《隐私协议》',
						icon: 'none',
						duration: 1000
					})
				} else {
					wx.getUserInfo({
						success: function(res) {
							console.log("微信数据", res.userInfo)
						}
					})
					uni.getUserProfile({
						desc: "获取你的昵称、头像、地区及性别",
						lang: 'zh_CN',
						success: async (res) => {
							console.log("用户信息", res.userInfo, )
							// return
							let res2 = await login({
								type: "2",
								code: that.logincode,
								name: res.userInfo.nickName,
								avatar: res.userInfo.avatarUrl

							})
							if (res2.code == 1) {
								console.log("微信登录", res2, res2.data.token.access_token)

								uni.setStorageSync("token", res2.data.token.access_token)
								uni.showToast({
									title: res2.msg,
									icon:"success",
									duration: 1500
								});
								// return;
								let res3 = await usermessage()
								setTimeout(()=>{
									if(res3.data.lock ==0){
										uni.switchTab({
											url: '/pages/home/index'
										})
									}else{
										if(res3.data.is_card == 1){
											//选择身份
											uni.redirectTo({
												url:'/pages/selectIdentity/index'
											})
										}else{
											//身份号验证
											uni.redirectTo({
												url:'/pages/authentication/index'
											})
										}
										
									}
									
								},2000)
								
								// return
								
								
								// console.log("用户信息", res3)
								// if (res3.data.is_card == 1 && res3.data.is_identity == 0 ) {
								// 		uni.redirectTo({
								// 			url: '/pages/authentication/index'
								// 		})
								// 	return
								// }

								// if (res3.data.is_card == 1 && res3.data.is_identity == 1) {
								// 		uni.switchTab({
								// 			url: '/pages/home/index'
								// 		})
								// 	return
								// }else{
								// 		uni.redirectTo({
								// 			url: '/pages/authentication/index'
								// 		})
								// }
								// if (that.typeFlag.is_card == 1 && that.typeFlag.is_card == 0 && that
								// 	.typeFlag.identity == 'member') {
								// 	setTimeout(() => {
								// 		uni.redirectTo({
								// 			url: '/pages/authentication/index'
								// 		})
								// 	}, 1000)
								// 	return
								// }
								// if (that.typeFlag.is_card == 1 && that.typeFlag.is_card == 0 && that
								// 	.typeFlag.identity == 'teacher') {
								// 	setTimeout(() => {
								// 		uni.redirectTo({
								// 			url: '/pages/authentication/index'
								// 		})
								// 	}, 1000)
								// 	return
								// } else {
								// 	uni.redirectTo({
								// 		url: '/pages/authentication/index'
								// 	})
								// }
							}
						},
						fail(e) {
							console.log("获取失败")
						}
					})


				}

			},
			gotoXiyi(type) {
				uni.redirectTo({
					// url:'/pages/xieyi/index'
					url: `/pages/xieyi/index?type=${type}`
				})
			}

		}
	}
</script>

<style scoped lang="scss">
	.login {

		// width: 100%;
		// height: 100%;
		.logoandtitle {
			width: 100%;
			height: 100%;
			margin-top: 270rpx;
			// margin-left: 286rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.logobox {
				width: 176rpx;
				height: 176rpx;

				.logoimg {
					width: 100%;
					height: 100%;
				}
			}

			.title {
				font-size: 36rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #333333;
				line-height: 42rpx;
				margin-top: 40rpx;
			}
		}

		.protocol {
			margin-top: 150rpx;
			margin-left: 94rpx;
			display: flex;

			.checked {}

			.protocoltext {
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 33rpx;
				display: flex;
				align-items: center;
			}
		}

		.loginbtn {
			width: 622rpx;
			height: 88rpx;
			background: #09BB07;
			border-radius: 200rpx 200rpx 200rpx 200rpx;
			opacity: 1;
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			line-height: 88rpx;
			text-align: center;
			margin: 36rpx auto;
		}
	}
</style>